<template>
  <div class="tabbar">
    <van-tabbar replace :border="false" @change="onChange" v-model="index">
      <van-tabbar-item
        :icon="v.icon"
        :to="v.url"
        v-for="(v, i) in tabbars"
        :key="i"
      >
        {{ v.name }}
      </van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import selects from 'js/selects'

const route = useRoute()

const index = ref(Number(localStorage.getItem('tabbarIndex') || 0))
const tabbars = ref(selects.tabbars)

getDefaultIndex()

function getDefaultIndex() {
  setTimeout(() => {
    index.value = tabbars.value.findIndex((v) => v.url === route.path)
  }, 100)
}

function onChange() {
  localStorage.setItem('tabbarIndex', index.value)
}
</script>

<style lang="scss" scoped></style>
